﻿ /* 
 * LoadProcess 类
 * 创建进度条
 * developed by tophy
 * 2005 7 12
 * 
 * 
 */
 class LoadProcess{
	//变量
	private var defaultTag:MovieClip;
	private var me:Object;
	
	private var defaultDrawType:Number;
	//构造函数
	public function LoadProcess (tag:MovieClip){
		init(tag);
		say("初始化完成。");
	}
	private function init(tag:MovieClip)
	{
		me=this;
		defaultTag=tag;
		defaultDrawType=0;
	}
	//功能函数部分
	// 创建加载进度条
	public function createLoadProcess(loadlist:Array,drawType:Number,then:Object,tag:MovieClip):Boolean{
		
		
		var root:MovieClip=defaultTag;
		var dt:Number=defaultDrawType;
		if(drawType!=undefined && drawType>=0){
			dt=drawType
		}
		if(tag==undefined && defaultTag==undefined)
		{
			say("没有生成loadProcess的目标。");
			return false;
		}else if(tag!=undefined && tag!=null){
			root=tag;
		}
		if((loadlist.length<=0 || typeof(loadlist)=="string" || typeof(loadlist)=="number") && typeof(loadlist)!="movieclip")
		{
			say("loadProcess的检测目标不正确。");
			return false;
		}
		
		
		var d:Number=root.getNextHighestDepth();
		if(d<0 || d>1000000)
		{
			d=1000000;
		}
		
		//----------------------------------------------------- create process
		
			root.createEmptyMovieClip ("loading"+d, d);
			var loading : MovieClip = root ["loading"+d];
			loading.okFun=then;
			loading.okTag=eval(then);
			loading.createTextField ("bfb", 1, - 30, 0, 30, 50);
			loading.bfb.autoSize = "right";
			loading.bfb.html = true;
			loading.bfb.htmlText = "0%";
			loading.tag = root;
			loading.me = me;
			loading.timeOut = 200;
			loading.loadTag=loadlist;
			loading.getTT = function ()
			{
				var rtTT : Number = 0;
				if(this.loadTag.length>0 && typeof(this.loadTag)=="object")
				{
					for (var i in this.loadTag)
					{
						rtTT += this.loadTag[i].getBytesTotal ();
					}
				}else
				{
					rtTT=this.loadTag.getBytesTotal();
				}
				return rtTT;
			}
			loading.getLD = function ()
			{
				var rtLD : Number = 0;
				if(this.loadTag.length>0 && typeof(this.loadTag)=="object")
				{
					for (var i in this.loadTag)
					{
						rtLD += this.loadTag[i].getBytesLoaded ();
					}
				}else
				{
					rtLD=this.loadTag.getBytesLoaded();
				}
				return rtLD;
			}
			loading.createGraphic = getDrawFunction(dt);
			loading.drawType=dt;
			loading.ld = 0;
			loading.slip = 30;
			
			loading.onEnterFrame = function ()
			{
				var tt : Number = this.getTT ();
				var ld : Number = this.getLD ();
				if (tt != undefined && tt.toString () != "NaN" && tt > 0 )
				{
					this.me.loadingFun(tt,ld);
					switch(this.dt)
					{
						case 1:
								if (tt != ld)
								{
									var bfbt : Number = int (ld * 10000 / tt) / 100;
									this.bfb.htmlText = "<font size='12' color='#999999'>" + bfbt.toString () + "%</font>";
									if (this.slip ++ > 45)
									{
										this.createGraphic (ld / tt);
										this.slip = 31;
									}
								}else
								{
									if (this.slip > 30)
									{
										this.slip = 30;
									}
									this.bfb.htmlText = "<font size='12' color='#999999'>100%</font>";
									this.createGraphic (ld / tt);
									if (this.slip > 0)
									{
										this.slip --;
									}else
									{
										this._alpha -= 5;
									}
									if (this._alpha <= 0)
									{
										if(typeof(this.okFun)=="function"){
											this.okFun();
										}else if(typeof(this.okFun)=="boolean")
										{
											this.okFun=true;
											this.okTag=true;
										}else if(typeof(this.okFun)=="movieclip"){
											this.okFun.play();
										}
										this.removeMovieClip ();
									}
								}
								this.timeOut = 200;
						break;
						
						case 2:
						break;
						
						default:
							
								if (tt != ld)
								{
									var bfbt : Number = int (ld * 10000 / tt) / 100;
									this.bfb.htmlText = "<font size='12' color='#999999'>" + bfbt.toString () + "%</font>";
									if (this.slip ++ > 45)
									{
										this.createGraphic (ld / tt);
										this.slip = 31;
									}
								}else
								{
									if (this.slip > 30)
									{
										this.slip = 30;
									}
									this.bfb.htmlText = "<font size='12' color='#999999'>100%</font>";
									this.createGraphic (ld / tt);
									if (this.slip > 0)
									{
										this.slip --;
									}else
									{
										this._alpha -= 5;
									}
									if (this._alpha <= 0)
									{
										if(typeof(this.okFun)=="function"){
											this.okFun();
										}else if(typeof(this.okFun)=="boolean")
										{
											this.okFun=true;
											this.okTag=true;
										}else if(typeof(this.okFun)=="movieclip"){
											this.okFun.play();
										}
										this.removeMovieClip ();
									}
								}
								this.timeOut = 200;
							
						break;
					}
				}else
				{
					this.timeOut --;
				}
				if (this.timeOut < 0)
				{
					this.me.timeOutFun (tt,ld);
					this.removeMovieClip();
				}
				
			};
		
		//========================================================================
		return true;
	}
	private function getDrawFunction(DrawType:Number):Function{
		var rf:Function;
		switch(DrawType){
			case 1:
				rf=function(x2:Number)
				{
					
					if(Math.abs(this.ld-x2)<0.01)
					{
						return;
					}
					var x=(x2-this.ld)*200-1;
					this.createEmptyMovieClip ("ld" + x2.toString () , this.getNextHighestDepth ());
					var t : MovieClip = this ["ld" + x2.toString ()];
					t.tag = this;
					t._x = this.ld * 200;
					t._y = -60;
					
					t.beginFill (0xaaaaaa, 100);
					t.moveTo (0, 0);
					t.lineTo (x, 0);
					t.lineTo (x, 13);
					t.lineTo (0, 13);
					t.lineTo (0, 0);
					t.endFill ();
					
					t.beginFill (int(Math.random()*(0xffffff)), 60);
					t.moveTo (0, 0);
					t.lineTo (x, 0);
					t.lineTo (x-1, 12);
					t.lineTo (0, 12);
					t.lineTo (0, 0);
					t.endFill ();
					
					t.y = 0;
					
					t.sj=0.6+Math.random()*0.3;
					
					t.xv=0;
					t.yv=0;
					t.ya=0.7;
					t.rv=0;
					
					t.onEnterFrame = function ()
					{
						
						if(this._parent.slip>20)
						{
							var ls=this.y-this._y;
							if(ls<=0)
							{
								if(Math.abs(t.yv)<1)
								{
									this._y=this.y;
									this.yv=0;
								}else{
									this._y=this.y;
									this.yv=-this.yv;
									this.yv*=this.sj;
								}
							}
							this.yv+=this.ya;
							this._y+=this.yv;
						}else if(this._parent.slip==20){
							this.yv=-(Math.random()*6+3);
							this.xv=Math.random()*4-2;
							this.rv=Math.random()-0.5;
						}else{
							this.yv+=this.ya;
							this._y+=this.yv;
							
							this._x+=this.xv;
							this._rotation+=this.rv*2;
							
							if(this._y>200)
							{
								this.removeMovieClip();
							}
						}
					}
					this.ld=x2;
				}
			break;
			
			case 2:
			break;
			
			
			default:
			rf=function (x2 : Number)
			{
				if(Math.abs(this.ld-x2)<0.01)
				{
					return;
				}
				var x = (x2 - this.ld) * 200-1;
				this.createEmptyMovieClip ("ld" + x2.toString () , this.getNextHighestDepth ());
				var t : MovieClip = this ["ld" + x2.toString ()];
				t.tag = this;
				t._alpha = 0;
				t._x = this.ld * 200;
				t._y = - 500;
				t.lineStyle (0, 0x999999, 100);
				t.beginFill (int(Math.random()*(0xffffff)), 50);
				t.moveTo (0, 0);
				t.lineTo (x, 0);
				t.lineTo (x, 13);
				t.lineTo (0, 13);
				t.lineTo (0, 0);
				t.endFill ();
				t.y = 0;
				t.onEnterFrame = function ()
				{
					var ly : Number = this.y - this._y;
					if (Math.abs (ly) < 0.1)
					{
						this._y = this.y;
						this._alpha = 100;
						delete this.onEnterFrame;
					}else
					{
						this._alpha += 10;
						this._y += (ly) / 3;
					}
				}
				this.ld = x2;
			}
			break;
		}
		return rf;
	}
	//固定函数
	private function say(o){
		trace("LoadProcess:"+o);
	}
	static public function HELP(){
		var h:String="";
		h+="--------------- HELP ---------------\r";
		h+="实例化本类的时候，需要一个loadingProcess生成位置movieclip，如下：\r";
		h+="_root.createEmptyMovieClip(\"loadingProcessParent\",1)\r";
		h+="var LP=new LoadProcess(_root.loadingProcessParent)\r";
		h+="当然，你也可以省略这个参数，但是，如果你省略的话，就必须在createLoadProcess函数中指明生成位置\r";
		h+="\r";
		h+="实例.createLoadProcess(loadlist:Array,drawType:Number,then:Object,tag:MovieClip):Boolean\r";
		h+="生成加载进度条\r";
		h+="loadlist为要监控的加载体，或是加载体数组（如果是数组，则监控的是整体的进度）\r";
		h+="drawType为进度条类型，目前有二种（0、1）\r";
		h+="then 为加载完成后执行的函数（如果是boolean类型，则返回加载是否成功的结果，如果是movieclip类型，则play这个movieclip）\r";
		h+="tag 进度条显示位置（父级），如果在本类初始化的时候没有明确确定，则在执行生成进度条函数时必须明确。\r";
		h+="\r";
		h+="实例.timeOutFun(tt,ld)\r";
		h+="加载超时时的函数，可在外部重载。tt为加载的总大小，ld为已经加载的字节数\r";
		h+="\r";
		h+="实例.loadingFun(tt,ld)\r";
		h+="加载过程中会不断调用的函数，可在外部重载。tt为加载的总大小,ld为已经加载的字节数\r";
		h+="\r";
		h+="\r";
		trace(h);
	}
}